package test;

import com.itstyle.quartz.kettleUtil.FileReadUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author ycj
 * @version 1.0
 * @description
 * @date 2021/1/7 17:05
 */
public class test2 implements Runnable{

    private String name="1";
    private int index=0;
    private ArrayList<String> list2 = new ArrayList();


    public test2() {
    }

    public static void main(String[] args) {
        ArrayList<String> filelist = null;
        test2 test2 = new test2();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 60L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(500));
        for (int i = 0; i < 10; i++) {
            executor.execute(test2);
        }
    }

    @Override
    public void run() {
        ArrayList<String> filelist = null;
        read(filelist);
    }

    public void read(ArrayList<String> filelist) {
        long starttime = System.currentTimeMillis();
        File file1 = new File("D:\\dianli\\工作材料\\J\\1.txt");
//        for (int i = 0; i <10000; i++) {
//            System.out.print(Thread.currentThread().getName()+"-----------"+i);
//            int a = 0;
//        }

        System.out.println(Thread.currentThread().getName()+"----------------------------------");

        File[] file = FileReadUtil.getFile("D:\\dianli\\工作材料\\k");
        for (int i = 0; i < file.length; i++) {
            String path = file[i].getPath();

            this.list2.add(path);

            if (list2!=null) {
                synchronized (this.name) {
                    System.out.println(Thread.currentThread().getName()+"----------------------------------"+index);
                    for (String s : this.list2){
                        if (s.equals(path)) {
                            index=1;
                        }
                    }
                }
            }

            if(index==1) {
                index=0;
                continue;
            }

            HashMap<String, List<String>> read = FileReadUtil.read(path);
            FileOutputStream stream = null;
            OutputStreamWriter writer = null;
            try {
                stream = new FileOutputStream(file1);
//                            writer = new OutputStreamWriter(stream,"UTF-8");
                List<String> value = read.get("value");
                for (String s : value) {
                    stream.write(s.getBytes("UTF-8"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "------" + path);

        }
        long endtime = System.currentTimeMillis();
        System.out.println("程序执行时间"+(endtime-starttime));
    }
}
